package cn.it.controller;
import cn.it.domain.User;
import cn.it.mapper.UserMapper;
import cn.it.service.IUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;


@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;

    @GetMapping("/getList")
    public List<User> getList(@RequestParam(value = "name",required = false)
                              String name,
                              @RequestParam(value = "sex",required = false)
                              Integer sex)
    {

        return userService.getList(name, sex);
    }

    @GetMapping("/getAll")
    public List<User> getAll(@RequestParam(value = "name",required = false)
                              String name,
                              @RequestParam(value = "sex",required = false)
                              Integer sex)
    {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        if(name != null && !name.equals("")){
            queryWrapper.like("name",name);
        }
        if(sex != null){
            queryWrapper.eq("sex",sex);
        }
        return userService.list(queryWrapper);
    }

    @GetMapping("/queryPage")
    public IPage<User> queryPage(@RequestParam(value = "name", required = false)
                                 String name,
                                 @RequestParam(value = "sex", required = false)
                                 Integer sex,
                                 @RequestParam(value = "pageNo", required = false,
                                         defaultValue = "1") Integer pageNo,
                                 @RequestParam(value = "pageSize", required = false,
                                         defaultValue = "10") Integer pageSize) {
// 构建查询条件
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (null != name && !"".equals(name)) {
            queryWrapper.like("name", name);
        }
        if (null != sex) {
            queryWrapper.eq("sex", sex);
        }
        queryWrapper.orderByDesc(true,"create_time");
// 构建分页对象，传入当前页及每页显示条数
        Page<User> page = new Page<>(pageNo, pageSize);
// 直接调用MyBatis-Plus IService提供的列表查询方法
        Page<User> pageList = userService.page(page, queryWrapper);
        return pageList;
    }
    /**
     * 新增
     *
     * @param user
     * @return
     */
    @PostMapping("/addUser")
    public String add(@RequestBody User user){
        user.setCreateTime(new Date());
        user.setCreateBy("admin");
        boolean result = userService.save(user);
        return result == true ? "新增成功":"新增失败";
    }
    /**
     * 根据主键修改
     *
     * @param user
     * @return
     */
    @PostMapping("/edit")
    public String edit(@RequestBody User user) {
        user.setUpdateTime(new Date());
        user.setUpdateBy("admin");
        boolean result = userService.updateById(user);
        return result ? "修改成功" : "修改失败";
    }
    /**
     * 根据主键删除
     *
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public String delete(@RequestParam(value = "id") Integer id) {
        System.out.println("=========================="+id);
        boolean result = userService.removeById(id);
        return result ? "删除成功" : "删除失败";
    }
}